﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using QT.Windows.Shell;

namespace SubNetWork
{
    public partial class Form1 : GlassForm
    {
        #region Attributes

        //默认网络信息
        protected static string IP = "192.168.0.0";
        protected static string Mask = "255.255.255.0";

        //标准网络信息
        protected static string NetWork_s = IP;
        protected static string Mask_s = Mask;
        protected static long IpCount_s = 0;

        //划分后网络信息
        protected static string NetWork_d = "0.0.0.0";
        protected static string Mask_d = "255.255.255.255";
        protected static long IpCount_d = 0;             

        private Thread myThread = null;
        private delegate void myDeleGateshowSub(string netid, string broadid, long ipcount);

        #endregion

        #region Form

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.tbIP.Text = IP;
            this.cbbMask.SelectedIndex = 23;
            this.lblMask.Text = "255.255.255.0";
            InitListView();
        }

        #endregion

        #region Event

        #region ComboBox

        private void cbbMask_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.lblMask.Text = QT.Net.IP.LongToIP(GetMask(Convert.ToInt32(this.cbbMask.SelectedItem)));
        }

        #endregion

        #region Button

        private void btnStart_Click(object sender, EventArgs e)
        {
            #region 判断IP合法性

            if (this.tbIP.Text.Length == 0)
            {
                MessageBox.Show("请输入IP地址");
                return;
            }
            if (!QT.Net.NetWork.CheckIP(this.tbIP.Text))
            {
                MessageBox.Show("请输入合法的IP地址");
                return;
            }
            if (QT.Net.IP.IPType(this.tbIP.Text).ToLower() == "local")
            {
                MessageBox.Show("本地回环地址不能进行子网划分");
                return;
            }
            if (QT.Net.IP.IsPrivate(this.tbIP.Text) == false)
            {
                if (MessageBox.Show("是否要对公网网络进行子网划分？", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }
            }

            #endregion

            #region 获取旧网段信息

            Mask_s = QT.Net.IP.GetdefaultMask(this.tbIP.Text);
            NetWork_s = QT.Net.IP.LongToIP(QT.Net.IP.IPToLong(this.tbIP.Text) & QT.Net.IP.IPToLong(Mask_s));
            IpCount_s = QT.Net.IP.IPToLong("255.255.255.255") - QT.Net.IP.IPToLong(Mask_s);

            #endregion

            #region 获取新网段信息

            Mask_d = this.lblMask.Text;
            NetWork_d = QT.Net.IP.LongToIP(QT.Net.IP.IPToLong(this.tbIP.Text) & QT.Net.IP.IPToLong(Mask));
            IpCount_d = QT.Net.IP.IPToLong("255.255.255.255") - QT.Net.IP.IPToLong(Mask_d);

            #endregion

            if (QT.Net.IP.IPToLong(Mask_s) > QT.Net.IP.IPToLong(Mask_d))
            {
                if (MessageBox.Show("您确认要进行超网划分吗?", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
                {
                    return;
                }
            }

            this.lvResult.Items.Clear();

            myThread = new Thread(new ThreadStart(ThreadSubNet));
            myThread.Start();
        }

        #endregion

        #region ListView

        private void InitListView()
        {
            this.lvResult.View = View.Details;
            this.lvResult.BeginUpdate();
            this.lvResult.Columns.Add("序号",50);
            this.lvResult.Columns.Add("网络号",100);
            this.lvResult.Columns.Add("广播地址",100);
            this.lvResult.Columns.Add("主机数",90);
            this.lvResult.EndUpdate();
        }

        #endregion

        #endregion

        #region Thread

        private void ThreadSubNet()
        {
            long ip = QT.Net.IP.IPToLong(NetWork_s);
            for (long i = 0; i < IpCount_s; i += IpCount_d + 1)
            {
                showSub(QT.Net.IP.LongToIP(ip + i), QT.Net.IP.LongToIP(ip + i + IpCount_d), IpCount_d - 1);
            }
        }

        #endregion

        #region IPMethod

        private void showSub(string netid,string broadid,long ipcount)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new myDeleGateshowSub(showSub), new object[] { netid, broadid, ipcount });
            }
            else
            {
                ListViewItem item=new ListViewItem();
                item.SubItems[0].Text = (this.lvResult.Items.Count + 1).ToString();
                item.SubItems.Add(netid);
                item.SubItems.Add(broadid);
                item.SubItems.Add(ipcount >= 0 ? ipcount.ToString() : "0");
                this.lvResult.Items.Add(item);
            }
        }

        #endregion
        
        #region Mask

        private long GetMask(int len)
        {
            Int64 mask = 0;
            string binary = GetBinary(len);

            mask = Convert.ToInt64(binary, 2);
            Mask = QT.Net.IP.LongToIP(mask);

            return mask;
        }

        private string GetBinary(int len)
        {
            string binary = "";

            for (int i = 0; i < len; i++)
            {
                binary += "1";
            }
            for (int i = len; i < 32; i++)
            {
                binary += "0";
            }

            return binary;
        }

        #endregion
    }
}
